package com.fjec.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.activity.entity.BizUserGift;
import com.fjec.activity.view.UserGiftVO;
import com.fjec.common.annotation.Json;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.exception.BizException;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import org.springframework.web.bind.annotation.*;

import com.fjec.activity.service.IBizUserGiftService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 001.用户礼物表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-18
 */
@RestController
@RequestMapping("/")
public class BizUserGiftController {

    @Resource
    private IBizUserGiftService service;

    @GetMapping("/y/gifts/getByUserId")
    public JsonResult getGiftsByUserId() {
        return service.getUserGiftsByUserId(AccountInfo.getUserId());

    }

    /**
     * 购买礼物
     * @param gifts
     * @return
     */
    @PostMapping("/y/buy/gifts")
    public JsonResult buyGifts(@Json List<UserGiftVO> gifts) {
        Long orderId = service.buyGifts(gifts);
        if (orderId != null) {
            if (orderId == -1)
                return JsonResult
                        .failure()
                        .code(EnumSysCode.ERR_3026.getCode())
                        .message("单笔支付金额不能超过5000元")
                        .build();
            Map<String, Object> result = new HashMap<>();
            result.put("orderId", orderId);
            return JsonResult.success().data(result).build();
        } else {
            return JsonResult.FAILURE;
        }

    }

    /**
     * 礼物支付成功前端回调接口
     * @param gifts
     * @param orderId
     * @return
     */
    @PostMapping("/y/buy/gifts/successfully")
    public JsonResult buyGiftsSuccessfully(@Json List<UserGiftVO> gifts, Long orderId) {
        int code = service.buyGiftsSuccessfully(gifts, orderId);
        if (code == 0)
            return JsonResult.SUCCESS;
        else
            return JsonResult.FAILURE;
    }

    /**
     * market service call this interface after set service order status to 2
     * @param boughtGifts
     * @return
     */
    @PostMapping("/put/user/gifts")
    public int putUserGifts(@RequestBody List<Map<String, Object>> boughtGifts, Long orderId) {
        int code = service.putUserGifts(boughtGifts, orderId);
        return code;
    }
}
